package com.songshuai.webapi.crm.service.impl;

import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.dto.feign.response.NavAndFuncByRoleResDto;
import com.songshuai.webapi.crm.dao.ConfigRoleMapper;
import com.songshuai.webapi.crm.dto.response.account.ConfigRolesResDto;
import com.songshuai.webapi.crm.model.ConfigRole;
import com.songshuai.webapi.crm.service.ConfigRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lhx
 * @Date: 2018/12/25 19:12
 */
@Service
public class ConfigRoleServiceImpl extends BaseServiceImpl implements ConfigRoleService {

    @Autowired
    private ConfigRoleMapper configRoleMapper;



    @Override
    public List<ConfigRolesResDto> queryAllList() {
        List<ConfigRolesResDto> result = new ArrayList<>();

        List<ConfigRole> configRoles = configRoleMapper.selectByEntityList(new ConfigRole() {{
            setIsDelete(false);
        }});
        if (CollectionUtils.isNotEmpty(configRoles)){
            List<Integer> roleIds = configRoles.stream().map(x -> x.getRoleId()).collect(Collectors.toList());
            QueryResponse<Map<Integer,List<NavAndFuncByRoleResDto>>> listQueryResponse = permissionClient.queryFunctionByRole(getBearerToken(),roleIds);
            if (null != listQueryResponse && ResultStatus.SUCCESS.getErrorCode() == listQueryResponse.getCode()){
                Map<Integer, List<NavAndFuncByRoleResDto>> map = listQueryResponse.getData();
                if((map != null && !map.isEmpty())){
                    for (ConfigRole configRole : configRoles) {
                        ConfigRolesResDto resDto = new ConfigRolesResDto();
                        resDto.setRoleName(configRole.getRoleName());
                        resDto.setDescription(configRole.getDescription());
                        resDto.setNavIds(map.get(configRole.getRoleId()));
                        result.add(resDto);
                    }
                }
            }
        }
        return result;
    }
}
